package co.fitstart.entity.courseware.exercise;

import java.beans.Transient;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.persistence.Column;
import javax.persistence.Entity;
import javax.persistence.FetchType;
import javax.persistence.GeneratedValue;
import javax.persistence.Id;
import javax.persistence.ManyToOne;
import javax.persistence.Table;

import org.springframework.format.annotation.DateTimeFormat;

import com.alibaba.fastjson.annotation.JSONField;
import com.fasterxml.jackson.annotation.JsonIdentityInfo;
import com.fasterxml.jackson.annotation.JsonIdentityReference;
import com.fasterxml.jackson.annotation.JsonIgnoreProperties;
import com.fasterxml.jackson.annotation.ObjectIdGenerators;

import co.fitstart.entity.courseware.fitnessgroup.BatchCourseware;
import co.fitstart.entity.principal.Principal;

/**
 * 周计划-课时计划
 * @author haiyan_xiao
 *
 */
@Entity
@Table(name = "planned_schedule")
@JsonIgnoreProperties(value={"hibernate_lazy_initializer","handler","fieldHandler"})
public class PlannedSchedule {
    @Id
    @GeneratedValue
    private Long id;
    
    /** 所属课件批次 */
    @ManyToOne(fetch = FetchType.LAZY, optional = false)
    @JSONField(serialize = false)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private BatchCourseware batch;
    
    /** 上课日期 */
    @DateTimeFormat(pattern = "yyyy-MM-dd")
    @org.hibernate.annotations.Type(type = "date")
    private Date plannedDay;
    
    /** 第几天 */
    private int day;
    
    /** 上课内容 */
    private String content;
    
    /** 上课评价/总结/记录 */
    private String description;
    
    /** 上课教练 */
    @ManyToOne(fetch = FetchType.LAZY, optional = true)
    @JsonIdentityInfo(generator=ObjectIdGenerators.PropertyGenerator.class, property="id")
    @JsonIdentityReference(alwaysAsId=true)
    private Principal coach;
    
    private Date createTime;
    
    @Column(name = "is_disabled")
    private boolean disabled;
    
    public PlannedSchedule() {
        
    }
    
    public PlannedSchedule(BatchCourseware batch, int day) {
        this.batch = batch;
        this.day = day;
        this.createTime = new Date();
        this.disabled = false;
    }
    
    /**
     * edit content
     * @param contents
     * @return
     */
    public PlannedSchedule editContent(List<ExerciseContent> contents) {
        if(contents != null && !contents.isEmpty()) {
            StringBuffer buffer = new StringBuffer();
            
            for(ExerciseContent content : contents) {
                buffer.append(content.ordinal() + ",");
            }
            
            this.content = buffer.toString();
        } else {
            this.content = null;
        }
        
        return this;
    }
    
    /**
     * 
     * @return
     */
    @Transient
    public List<ExerciseContent> getContentList() {
        List<ExerciseContent> contents = new ArrayList<>();
        
        if(this.content != null) {
            String[] temps = content.split(",");
            
            if(temps != null && temps.length > 0) {
                for(String temp : temps) {
                    contents.add(ExerciseContent.ordinalOf(Integer.parseInt(temp)));
                }
            }
        }
        
        return contents;
    }

    public String getContentString() {
        StringBuffer buffer = new StringBuffer();
        
        if(this.content != null) {
            String[] temps = content.split(",");
            
            if(temps != null && temps.length > 0) {
                for(String temp : temps) {
                    ExerciseContent exerciseContent = ExerciseContent.ordinalOf(Integer.parseInt(temp));
                    
                    if(exerciseContent != null) {
                        buffer.append(exerciseContent.getDescription() + "+");
                    }
                }
            }
        }
        
        if(buffer.length() == 0) {
            return null;
        } else {
            return buffer.substring(0, buffer.length() - 1);
        }
    }

    /**
     * @return the id
     */
    public Long getId() {
        return id;
    }

    /**
     * @return the batch
     */
    public BatchCourseware getBatch() {
        return batch;
    }

    /**
     * @return the plannedDay
     */
    public Date getPlannedDay() {
        return plannedDay;
    }

    /**
     * @return the day
     */
    public int getDay() {
        return day;
    }

    /**
     * @return the content
     */
    public String getContent() {
        return content;
    }

    /**
     * @return the description
     */
    public String getDescription() {
        return description;
    }

    /**
     * @return the coach
     */
    public Principal getCoach() {
        return coach;
    }

    /**
     * @return the createTime
     */
    public Date getCreateTime() {
        return createTime;
    }

    /**
     * @return the disabled
     */
    public boolean isDisabled() {
        return disabled;
    }

    /**
     * @param id the id to set
     */
    public void setId(Long id) {
        this.id = id;
    }

    /**
     * @param batch the batch to set
     */
    public void setBatch(BatchCourseware batch) {
        this.batch = batch;
    }

    /**
     * @param plannedDay the plannedDay to set
     */
    public void setPlannedDay(Date plannedDay) {
        this.plannedDay = plannedDay;
    }

    /**
     * @param day the day to set
     */
    public void setDay(int day) {
        this.day = day;
    }

    /**
     * @param content the content to set
     */
    public void setContent(String content) {
        this.content = content;
    }

    /**
     * @param description the description to set
     */
    public void setDescription(String description) {
        this.description = description;
    }

    /**
     * @param coach the coach to set
     */
    public void setCoach(Principal coach) {
        this.coach = coach;
    }

    /**
     * @param createTime the createTime to set
     */
    public void setCreateTime(Date createTime) {
        this.createTime = createTime;
    }

    /**
     * @param disabled the disabled to set
     */
    public void setDisabled(boolean disabled) {
        this.disabled = disabled;
    }
}
